package com.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.dao.manager.RegistrationRecordImgMapper;
import com.dao.manager.RegistrationRecordMapper;
import com.pojo.RegistrationRecord;
import com.pojo.RegistrationRecordImg;
import com.service.RegistrationRecordService;
import com.utils.UUIDUtils;
import com.utils.UploadUtils;

@Service
public class RegistrationRecordServiceImp implements RegistrationRecordService {

	@Resource
	private RegistrationRecordMapper registrationRecordMapper;
	
	@Resource
	private RegistrationRecordImgMapper registrationRecordImgMapper;
	
	@Value("${img.upload.path}")
	private String imgUploadPath;
	@Value("${img.access.path}")
	private String imgAccessPath;

	@Override
	public int addRegistrationRecordInfo(Map<String, String> paramsMap, MultipartFile[] imgfiles) {
		try {
			String uuid = UUIDUtils.getUUID();
			// 七牛上传图片
			//List<String> imgPaths = UploadUtils.uploadQiniuImg(imgfiles, imgUploadPath, imgAccessPath);
			RegistrationRecord entity = new RegistrationRecord();
			entity.setUuid(uuid);
			entity.setRegistrationid(Integer.valueOf(paramsMap.get("registrationid")));
			entity.setContext(paramsMap.get("context"));
			entity.setAddress(paramsMap.get("address"));
			entity.setUsername(paramsMap.get("username"));
			entity.setType(paramsMap.get("type"));
			entity.setCreatetime(new Date());
			registrationRecordMapper.addRegistrationRecordInfo(entity);

			//imgurl 已经上传的图片路径
			String imgurl = paramsMap.get("imgurl");
			RegistrationRecordImg entity1 = new RegistrationRecordImg();
			if (imgurl != null && !"".equals(imgurl)) {
				for (String img : imgurl.split(",")) {
					entity1.setImg(img);
					entity1.setRecordid(entity.getId());
					registrationRecordImgMapper.addRegistrationRecordImg(entity1);
				}
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int deleteRegistrationRecordInfo(Map<String, String> paramsMap) {
		try {
			return registrationRecordMapper.deleteRegistrationRecordInfo(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	public int updateRegistrationRecordInfo(Map<String, String> paramsMap, MultipartFile[] imgfiles) {
		try {
			String uuid = paramsMap.get("uuid");
			List<String> imgPaths = UploadUtils.uploadQiniuImg(imgfiles, imgUploadPath, imgAccessPath);
			RegistrationRecord entity = new RegistrationRecord();
			if (imgPaths != null && !imgPaths.isEmpty()) {
				entity.setImg(imgPaths.get(0));
			} else {
				entity.setImg("");
			}
			entity.setUuid(uuid);
			entity.setContext(paramsMap.get("context"));
			entity.setAddress(paramsMap.get("address"));
			entity.setUsername(paramsMap.get("username"));
			entity.setPhone(paramsMap.get("phone"));
			entity.setType(paramsMap.get("type"));
			entity.setCreatetime(new Date());
			registrationRecordMapper.updateRegistrationRecordInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> getRegistrationRecordInfo(Map<String, String> paramsMap) {
		String page = paramsMap.get("page");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		String num = paramsMap.get("num");
		if (num == null || "".equals(num)) {
			num = "10";
			paramsMap.put("num", num);
		}
		int start = (Integer.parseInt(page) - 1) * Integer.parseInt(num);
		paramsMap.put("start", start + "");
		return registrationRecordMapper.getRegistrationRecordInfo(paramsMap);
	}

	@Override
	public List<Map<String, String>> getRegistrationRecordByUuid(Map<String, String> paramsMap) {
		return registrationRecordMapper.getRegistrationRecordByUuid(paramsMap.get("uuid"));
	}

	@Override
	public Map<String, String> getRegistrationRecordByDeviceId(Map<String, String> paramsMap) {
		return registrationRecordMapper.getRegistrationRecordByDeviceId(paramsMap.get("deviceid"));
	}

	@Override
	public Map<String, String> getPassword(Map<String, String> paramsMap) {
		return registrationRecordMapper.getPassword(paramsMap);
	}

} 
